package com.demoworld;

/**
 * Created by francis on 2017/7/5.
 */
public class JLinkedListQueue implements IQueue {
    public JLinkedListQueue() {
        tail = null;
        head = null;
    }

    @Override
    public void push(int n) {
        JLinkedListStackNode node = new JLinkedListStackNode();
        node.value = n;
        if (this.isEmpty()) {//添加第一个元素
            tail = node;
            head = node;
            return;
        }
        if (head == tail) {
            head.next = node;
            tail = node;
            return;
        }
        tail.next = node;
        tail = node;
    }

    @Override
    public int pop() {
        if (this.isEmpty()) {
            return -1;//为了测试， 在push时 不要push -1
        }
        int n = head.value;
        head = head.next;
        if (head == null) {
            tail = null;
        }
        return n;
    }

    @Override
    public boolean isEmpty() {
        return (tail == null && head == null);
    }

    public JLinkedListStackNode tail;
    public JLinkedListStackNode head;

    class JLinkedListStackNode {
        public JLinkedListStackNode() {
            next = null;
        }

        public JLinkedListStackNode next;
        public int value;
    }
}
